﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using JCode.DBUtility;
using JCode.IDAL;
namespace JCode.SQLServerDAL
{
    public class Code : ICode
    {
        public int Add(JCode.Model.CodeInfo model)
        {
            
            SqlParameter[] parameters = {
					new SqlParameter("@CodeID", SqlDbType.Int,4),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@Keys", SqlDbType.NVarChar,200),
					new SqlParameter("@Content", SqlDbType.NText),
					new SqlParameter("@DateCreated", SqlDbType.SmallDateTime),
					new SqlParameter("@DateUpdate", SqlDbType.SmallDateTime)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.CategoryID;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.Keys;
            parameters[4].Value = model.Content;
            parameters[5].Value = model.DateCreated;
            parameters[6].Value = model.DateUpdate;
            SQLHelper.ExecNonQueryReturn_Proc("J_Code_Add", parameters);
            return (int)parameters[0].Value;

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public JCode.Model.CodeInfo GetModel(int CodeID)
        {
           

            JCode.Model.CodeInfo model = new JCode.Model.CodeInfo();
         
            DataSet ds =    SQLHelper.GetDataSet("select * from Code where CodeID='"+CodeID+"'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CodeID"].ToString() != "")
                {
                    model.CodeID = int.Parse(ds.Tables[0].Rows[0]["CodeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "")
                {
                    model.CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.Keys = ds.Tables[0].Rows[0]["Keys"].ToString();
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                if (ds.Tables[0].Rows[0]["DateCreated"].ToString() != "")
                {
                    model.DateCreated = (DateTime)ds.Tables[0].Rows[0]["DateCreated"];
                }
                if (ds.Tables[0].Rows[0]["DateUpdate"].ToString() != "")
                {
                    model.DateUpdate =(DateTime)ds.Tables[0].Rows[0]["DateUpdate"];
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CodeID,CategoryID,Title,Keys,Content,DateCreated,DateUpdate ");
            strSql.Append(" FROM Code ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.GetDataSet(strSql.ToString());

        }
    }
}
